package com.page.action.status;

import java.util.List;

import javax.xml.ws.Action;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.page.action.base.BaseAction;
import com.page.common.vo.ServerStatusVO;
import com.page.service.ServerStatusService;

public class StatusAction extends BaseAction {

    private static final long serialVersionUID = 1L;
    private ServerStatusService serverStatusService;
    
    public void setServerStatusService(ServerStatusService serverStatusService) {
        this.serverStatusService = serverStatusService;
    }

    @Action
    public String showPageAction() {
        return SUCCESS;
    }
    
    @Action
    public String showAllAction() {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        
        List<ServerStatusVO> servers = serverStatusService.getServersStatus();
        for (ServerStatusVO server : servers) {
            JSONObject object = new JSONObject();
            object.put("server", server.getAddr());
            object.put("conns", server.getConns());
            object.put("used", server.getUsed());
            object.put("max_threads", server.getMaxThreads());
            object.put("curr_threads", server.getCurrThreads());
            object.put("busy_threads", server.getBusyThreads());
            object.put("qlen", server.getQlen());
            object.put("load", server.getLoad());
            jsonArray.add(object);
        }
        
        jsonObject.put("servers", jsonArray);
        return reply(jsonObject) == true ? SUCCESS : ERROR;
    }
    
}
